package com.example.seey2023.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.example.seey2023.entity.User;
import com.example.seey2023.service.UserService;
import com.example.seey2023.vo.DataView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/toRegister")
    public String toRegister() {
        return "user/register";
    }

    /**
     * 验证码
     *
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("/login/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        // 1.验证码对象（宽，高，验证码位数，干扰线条数）
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 10);
        // 2.放入到session
        session.setAttribute("code", captcha.getCode());
        // 3.输出验证码
        ServletOutputStream outputStream = response.getOutputStream();
        captcha.write(outputStream);
        // 4.关闭输出流
        outputStream.close();
    }

    /**
     * 登录
     */
    @RequestMapping("/login/login")
    @ResponseBody
    public DataView login(String username, String password, String code, HttpSession session) {
        DataView dataView = new DataView();
        // 1.判断验证码
        String sessionCode = (String) session.getAttribute("code");
        if (code != null && sessionCode.equals(code)) {
            // 2.session登录逻辑
            // User user =  userService.login(username,password);
            // 2.Shiro登录
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            try {
                subject.login(token);
            } catch (Exception e) {
                dataView.setCode(100);
                dataView.setMsg("密码或用户名输入错误！");
                return dataView;
            }


            User user = (User) subject.getPrincipal();
            // 3. 判断
            if (user != null) {
                dataView.setCode(200);
                dataView.setMsg("登录成功！");
                session.setAttribute("user", user);
                return dataView;
            } else {
                dataView.setCode(100);
                dataView.setMsg("登陆失败，用户名或密码错误，请重新输入！");
                return dataView;
            }
        }
        dataView.setCode(100);
        dataView.setMsg("验证码错误，请重新输入！");
        return dataView;
    }


    /**
     * 退出登录
     *
     * @return
     */
    @RequestMapping("/login/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

}

